package com.itheima.web.controller;

import com.itheima.domain.system.SysLog;
import com.itheima.service.store.MemberService;
import com.itheima.service.store.OrdersService;
import com.itheima.service.store.ProductService;
import com.itheima.service.store.impl.MemberServiceImpl;
import com.itheima.service.store.impl.OrdersServiceImpl;
import com.itheima.service.store.impl.ProductServiceImpl;
import com.itheima.service.system.PermissionService;
import com.itheima.service.system.RoleService;
import com.itheima.service.system.SysLogService;
import com.itheima.service.system.UserService;
import com.itheima.service.system.impl.PermissionServiceImpl;
import com.itheima.service.system.impl.RoleServiceImpl;
import com.itheima.service.system.impl.SysLogServiceImpl;
import com.itheima.service.system.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.UUID;

public class BaseServlet extends HttpServlet {

    protected SysLogService sysLogService;
    protected PermissionService permissionService;
    protected RoleService roleService;
    protected UserService userService;
    protected ProductService productService;
    protected OrdersService ordersService;
    protected MemberService memberService;

    /*
          init: 只执行一次
     */
    public void init() throws ServletException {
        sysLogService = new SysLogServiceImpl();
        permissionService = new PermissionServiceImpl();
        roleService = new RoleServiceImpl();
        userService = new UserServiceImpl();
        productService = new ProductServiceImpl();
        ordersService = new OrdersServiceImpl();
        memberService = new MemberServiceImpl();
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //0. 设置请求编码, 编码处理一定要设置在获取参数之前
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");

            //1. 获取method参数
            String methodName = request.getParameter("operation");

            //获取method参数 userServlet?method=findAll/add

            /** 日志记录 方法开始时间 */
            Long start = System.currentTimeMillis();

            //2. 基于反射动态调用类的方法
            //2.1 获取当前类的Class字节码对象
            Class clazz = this.getClass();
//            System.out.println(this);
            //2.2 获取方法对象
            Method method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            // 执行方法
            method.invoke(this, request, response);  // this.update(request, response);

            /** 日志记录 方法结束时间 */
            Long end = System.currentTimeMillis();

            /** 日志记录 */
            String id = UUID.randomUUID().toString().replace("-", "").toUpperCase().substring(23);
            Date visitTime = new Date();
//            User user = (User) request.getSession().getAttribute("user");
//            String username = user.getUsername();
            String username = "Tlias";
            String ip = request.getRemoteAddr();
            String url = request.getRequestURI();
            Long executionTime = end - start;
//            String id, Date visitTime, String username, String ip, String url, Long executionTime, String method
            sysLogService.save(new SysLog(id, visitTime, username, ip, url, executionTime, methodName));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
